package gui.consumption;

import gui.alert;
import pojo.Card;
import pojo.ConsumptionWithName;
import service.cardService;
import service.consumptionService;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class add_Consumption {
    private JTextField textField1;
    private JButton 按id查询Button;
    private JTextField textField2;
    private JButton 按姓名查找Button;
    private JPanel query;
    private JTextArea result;
    private JButton consumption_Button;
    private JTextField consumption_input;
    private JTextField used_info;
    private JLabel discount_label;
    private Card card_tmp;

    public add_Consumption() {
        按id查询Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Card card = cardService.queryByid(Integer.parseInt(textField1.getText()));
                if (card != null && card.getIsUseful() == 1) {
                    card_tmp = card;
                    result.setText(card.toString());
                    discount_label.setText(card_tmp.getDiscountRate() + "");
                } else {
                    result.setText("无结果");
                }

            }
        });
        按姓名查找Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Card card = cardService.queryByName(textField2.getText());
                if (card != null && card.getIsUseful() == 1) {
                    card_tmp = card;
                    result.setText(card.toString());
                    discount_label.setText(card_tmp.getDiscountRate() + "");
                } else {
                    result.setText("无结果");
                }
            }
        });
        consumption_Button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                ConsumptionWithName consumptionWithName = new ConsumptionWithName();
                consumptionWithName.setCardId(card_tmp.getId());
                consumptionWithName.setInfo(used_info.getText());
                consumptionWithName.setMoney(Double.parseDouble(consumption_input.getText()) * card_tmp.getDiscountRate());

                if (consumptionService.insert(consumptionWithName) == 1) {
                    alert.show("消费成功");
                } else {
                    alert.show("消费失败");
                }

            }
        });
    }

    public static void show() {
        JFrame frame = new JFrame("查询");
        frame.setContentPane(new add_Consumption().query);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
